<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	function __construct()
    {
        parent::__construct();
        $this->load->model('admin_model');       
    }

    function index()
    {
    	$this->load->view('elements/admin_header');
    	$this->load->view('admin/login');
    	$this->load->view('elements/admin_footer');
    }

    function login()
    {
        $last_login = date('Y-m-d');
        $login_details = array('username' => $_POST['username'],
                               'password' => md5($_POST['password']));

        $data['login'] = $this->admin_model->login($login_details);
        if(!empty($data['login']))
        {
            $data['login']['is_admin'] = 1;
            $this->session->set_userdata($data['login']);
            
            $this->db->update('users',array('last_login' => $last_login));
            $this->load->view('elements/admin_header');
            $this->load->view('elements/admin_sidebar');
            $this->load->view('admin/dashboard');
            $this->load->view('elements/admin_footer');
        }
        else
        {
            $this->load->view('elements/admin_header');
            $this->load->view('admin/login');
            $this->load->view('elements/admin_footer');
        }
    }

    function dashboard()
    {
        if($this->session->userdata('is_admin') == 1)
        {
            $data['active'] = 'dashboard';
            $this->load->view('elements/admin_header');
            $this->load->view('elements/admin_sidebar',$data);
            $this->load->view('admin/dashboard');
            $this->load->view('elements/admin_footer');
        }
        else
        {
            redirect('admin');
        }
    }

    function slider_options()
    {
         if($this->session->userdata('is_admin') == 1)
        {
            $data['slider_data'] = $this->admin_model->get_slider_data();
            $data['active'] = 'slider';
            //pr($data);exit;
            $this->load->view('elements/admin_header');
            $this->load->view('elements/admin_sidebar',$data);
            $this->load->view('admin/slider_options',$data);
            $this->load->view('elements/admin_footer');
        }
        else
        {
            redirect('admin');
        }
    }

    function add_slider_images()
    {
        $files = $_FILES['files']; 
        pr($_POST);
        $data['info'] = $this->admin_model->add_slider_images($_POST,$files);
        redirect('admin/slider_options');
    }

    function get_slider_info()
    {
        $id = $_POST['id'];
        $data['slider_data'] = $this->admin_model->get_slider_info($id);
        echo json_encode($data);
    }

    function edit_slider($id)
    {
        if($this->session->userdata('is_admin') == 1)
        {
            $data['active'] = 'slider';
            $data['slider_data'] = $this->admin_model->get_slider_info($id);
            if(empty($data['slider_data']))
                show_error('Data not found');
            $this->load->view('elements/admin_header',$data);
            $this->load->view('elements/admin_sidebar');
            $this->load->view('admin/edit_slider',$data);
            $this->load->view('elements/admin_footer');
        }else{
            redirect('admin');
        }
    }

    function edit_slider_images()
    {
        //pr($_POST);exit;
        if(isset($_FILES['files'])){
            $files = $_FILES['files'];
        }
        $id = $_POST['id'];
        $check = '';
        /*if(isset($_POST['checkbox']))
        {
            $check = 1;
        }*/
        $data['info'] = $this->admin_model->get_slider_info($id);
       /* $path = "./uploads/".$data['info']['image_name'];
        unlink($path);*/
        $this->admin_model->update_slider($_POST,$files,$id);
        redirect('admin/slider_options');
    }

    function del_slider($id)
    {
        $data['slider_data'] = $this->admin_model->del_slider($id);
        redirect('admin/slider_options');
    }

    function logout()
    {
        $this->session->sess_destroy();
        redirect('admin');
    }

}